package com.huabo.audit.service.impl;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.hbfk.entity.DealUserToken;
import com.hbfk.entity.TblStaffUtil;
import com.hbfk.util.JsonBean;
import com.hbfk.util.ResponseFormat;
import com.huabo.audit.config.DateBaseConfig;
import com.huabo.audit.oracle.entity.TblNbsjProject;
import com.huabo.audit.oracle.entity.TblOrganization;
import com.huabo.audit.oracle.entity.TblStaff;
import com.huabo.audit.oracle.mapper.TblOrganizationMapper;
import com.huabo.audit.oracle.mapper.TblStaffMapper;
import com.huabo.audit.service.TblStaffService;
import com.huabo.audit.util.PageInfo;

@Service
public class TblStaffServiceImpl implements TblStaffService {
	
	@Resource
	public TblOrganizationMapper tblOrganizationMapper;
	
	@Resource
	public TblStaffMapper tblStaffMapper;

	@Override
	public Map<String, Object> findAllPageBeanPid(String pid, Integer pageNumber, Integer pageSize, String token,
			String staffId) {
		if(DateBaseConfig.DATABASETYPE.equals("Oracle")) {
			Map<String, Object> resultMap = new HashMap<String, Object>(0);
			try {
				Map<String, Object> dataMap = new HashMap<String, Object>(0);
				TblStaffUtil user = DealUserToken.parseUserToken(token);
				if (user == null) {
					resultMap.put("code", "0");
					resultMap.put("msg", "用户已失效！");
					return resultMap;
				}
				TblOrganization attribute = new TblOrganization();
				if (pid != null && !pid.equals("")) {
					attribute = tblOrganizationMapper.findById(new BigDecimal(pid));
				} else {
					attribute.setOrgid(user.getCurrentOrg().getOrgid());
					attribute.setOrgtype(user.getCurrentOrg().getOrgtype());
				}
				PageInfo<TblStaff> pageInfo = new PageInfo<TblStaff>();
				pageInfo.setCurrentPage(pageNumber);
				pageInfo.setPageSize(pageSize);
				pageInfo.setTlist(tblStaffMapper.findAllPageBeanPid(pageInfo, attribute));
				pageInfo.setTotalRecord(tblStaffMapper.findAllCountPageBeanPid(pageInfo, attribute));
				dataMap.put("pageInfo", pageInfo);
				dataMap.put("attribute", attribute);
				resultMap.put("code", "1");
				resultMap.put("msg", "访问接口成功");
				resultMap.put("pageInfo", dataMap);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return resultMap;
		} else {
			Map<String, Object> resultMap = new HashMap<String, Object>(0);
			try {
				Map<String, Object> dataMap = new HashMap<String, Object>(0);
				TblStaffUtil user = DealUserToken.parseUserToken(token);
				if (user == null) {
					resultMap.put("code", "0");
					resultMap.put("msg", "用户已失效！");
					return resultMap;
				}
				TblOrganization attribute = new TblOrganization();
				if (pid != null && !pid.equals("")) {
					attribute = tblOrganizationMapper.findById(new BigDecimal(pid));
				} else {
					attribute.setOrgid(user.getCurrentOrg().getOrgid());
					attribute.setOrgtype(user.getCurrentOrg().getOrgtype());
				}
				PageInfo<TblStaff> pageInfo = new PageInfo<TblStaff>();
				pageInfo.setCurrentPage(pageNumber);
				pageInfo.setPageSize(pageSize);
				pageInfo.setTlist(tblStaffMapper.findAllPageBeanPid(pageInfo, attribute));
				pageInfo.setTotalRecord(tblStaffMapper.findAllCountPageBeanPid(pageInfo, attribute));
				dataMap.put("pageInfo", pageInfo);
				dataMap.put("attribute", attribute);
				resultMap.put("code", "1");
				resultMap.put("msg", "访问接口成功");
				resultMap.put("pageInfo", dataMap);
			} catch (Exception e) {
				e.printStackTrace();
			}
			return resultMap;
		}
	}

	@Override
	public JsonBean findByAllPageBean(String useranme, Integer pageNumber, Integer pageSize, String token,String realname)throws Exception {
		TblStaffUtil loginStaff = DealUserToken.parseUserToken(token);
		if(loginStaff == null) {
			return ResponseFormat.retParam(0,20006,null);
		}
		
		PageInfo<TblStaff> pageInfo = new PageInfo<TblStaff>();
		pageInfo.setCurrentPage(pageNumber);
		pageInfo.setPageSize(pageSize);
		pageInfo.setTlist(tblStaffMapper.selectByOrgidListPageInfo(pageInfo, loginStaff.getCurrentOrg().getOrgid(),useranme,realname));
		pageInfo.setTotalRecord(tblStaffMapper.selectByOrgidCountPageInfo(loginStaff.getCurrentOrg().getOrgid(),useranme,realname));
		Map<String,Object> resultMap = new HashMap<String,Object>(0);
		resultMap.put("pageInfo", pageInfo);
    	return ResponseFormat.retParam(1,200,resultMap);
		
	}

	@Override
	public JsonBean findUsrDetail(String token, Integer staffid) throws Exception {
		TblStaffUtil loginStaff = DealUserToken.parseUserToken(token);
		if(loginStaff == null) {
			return ResponseFormat.retParam(0,20006,null);
		}
		Map<String,Object> resultMap = new HashMap<String,Object>(0);
		TblStaff plan = this.tblStaffMapper.selectById(staffid);
		try {
			TblOrganization linkOrg = this.tblOrganizationMapper.selectCompanyInfoByDeptId(plan.getOrgid());
			TblOrganization linkdept = this.tblOrganizationMapper.findById(plan.getOrgid());
			plan.setLinkDetp(linkdept);
			plan.setLinkOrg(linkOrg);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		resultMap.put("usrInfo", plan);
		return ResponseFormat.retParam(1,200,resultMap);
	}

}
